﻿using Android.App;
using Android.Widget;
using HeWeatherAPI.Model;
using System.Collections.Generic;
using Weather;

namespace Weather.Adapter
{
    public class HourlyAdapter : BaseAdapter<HourlyForcast>
    {

        class ViewHolder : Java.Lang.Object
        {
            public TextView TimeView { get; set; }
            public TextView TempView { get; set; }
        }

        protected Activity context = null;
        protected IList<HourlyForcast> hourlys;

        public HourlyAdapter(Activity context, IList<HourlyForcast> hourlys)
            : base()
        {
            this.context = context;
            this.hourlys = hourlys;
        }

        public override HourlyForcast this[int position]
        {
            get { return hourlys[position]; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get
            {
                {
                    if (hourlys.Count != 0)
                    {
                        var headerTime = context.FindViewById<TextView>(Resource.Id.HeaderTime);
                        var headerTemp = context.FindViewById<TextView>(Resource.Id.HeaderTemp);
                        headerTime.Text = "Time";
                        headerTemp.Text = "Temp.";
                    }
                    return hourlys.Count;
                }
            }
        }

        public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
        {
            // Get our object for position
            var item = hourlys[position];

            //Try to reuse convertView if it's not  null, otherwise inflate it from our item layout
            // gives us some performance gains by not always inflating a new view
            // will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell()

            ViewHolder vh = null;
            if (convertView == null)
            {
                convertView = context.LayoutInflater.Inflate(Resource.Layout.HourlyItem, parent, false);
                vh = new ViewHolder();
                vh.TimeView = convertView.FindViewById<TextView>(Resource.Id.TxtTime);
                vh.TempView = convertView.FindViewById<TextView>(Resource.Id.TxtTemperature);
                convertView.Tag = vh;
            }
            else
            {
                vh = (ViewHolder)convertView.Tag;

            }

            vh.TempView.Text = string.Format("{0}℃",item.Temperature.ToString());
            vh.TimeView.Text = item.Time.ToString("HH:mm:ss");
            //Finally return the view
            return convertView;
        }
    }
}